package com.mouse.workflow.activiti;

import com.mouse.workflow.activiti.services.MyService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by cwx183898 on 2017/8/1.
 */
@SpringBootApplication
@ComponentScan(basePackages = {"com.mouse"}, basePackageClasses = {Application.class})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner init(final MyService myService) {

        return new CommandLineRunner() {
            public void run(String... strings) throws Exception {
                myService.createDemoUsers();
            }
        };

    }

//    @Bean
//    InitializingBean usersAndGroupsInitializer(final IdentityService identityService) {
//
//        return new InitializingBean() {
//            public void afterPropertiesSet() throws Exception {
//                Group group = identityService.newGroup(UUID.randomUUID().toString());
//                group.setName("users");
//                group.setType("security-role");
//                identityService.saveGroup(group);
//                User admin = identityService.newUser(UUID.randomUUID().toString());
//                admin.setPassword("admin");
//                identityService.saveUser(admin);
//            }
//        };
//    }

//    @Bean
//    CommandLineRunner init(final RepositoryService repositoryService,
//                           final RuntimeService runtimeService,
//                           final TaskService taskService) {
//        return new CommandLineRunner() {
//
//            public void run(String... strings) throws Exception {
//                System.out.println("Number of process definitions : "
//                        + repositoryService.createProcessDefinitionQuery().count());
//                System.out.println("Number of tasks : " + taskService.createTaskQuery().count());
//                runtimeService.startProcessInstanceByKey("oneTaskProcess");
//                System.out.println("Number of tasks after process start: " + taskService.createTaskQuery().count());
//            }
//        };
//    }
}
